package com.aiyige.page.publish.uploadfile;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.db.DBHelper;
import com.aiyige.base.db.dao.InfoModelDao;
import com.aiyige.base.db.dao.LearnVideoModelDao;
import com.aiyige.base.db.dao.NormalVideoModelDao;
import com.aiyige.base.db.dao.PhotoModelDao;
import com.aiyige.base.db.dao.SpecialTopicDBModelDao;
import com.aiyige.base.db.dao.UploadFileDao;
import com.aiyige.base.db.table.InfoModel;
import com.aiyige.base.db.table.LearnVideoModel;
import com.aiyige.base.db.table.NormalVideoModel;
import com.aiyige.base.db.table.PhotoModel;
import com.aiyige.base.db.table.SpecialTopicDBModel;
import com.aiyige.base.db.table.UploadFile;
import com.aiyige.page.publish.info.adapter.PublishInfoAdapter;
import com.aiyige.page.publish.photo.model.PublishPhotoItem;
import com.aiyige.page.publish.uploadfile.adapter.UploadFileAdapter;
import com.aiyige.page.publish.util.PublishUtil;
import com.aiyige.utils.AccountUtil;
import com.aiyige.utils.widget.CommonBottomDialogFragment;
import com.aiyige.utils.widget.articleview.model.ArticleNode;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Callable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import timber.log.Timber;

@Route(path = ARouterConfig.UploadFilePage)
public class UploadFilePage extends AppCompatActivity {
    public static final String TAG = UploadFilePage.class.getSimpleName();
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.titleActionBtn)
    Button titleActionBtn;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Autowired
    String parentId;
    @Autowired
    int parentType;
    Dao<UploadFile, Integer> uploadFileDao;
    BroadcastReceiver broadcastReceiver;
    volatile boolean querying = false;
    UploadFileAdapter uploadFileAdapter;
    AlertDialog cancelPublishDialog;
    CommonBottomDialogFragment deleteDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_upload_file);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        deleteDialog = CommonBottomDialogFragment.newBuilder()
                .layoutResId(R.layout.upload_file_delete_dialog)
                .viewIdList(Arrays.asList(R.id.deleteBtn, R.id.cancelBtn))
                .listener(new CommonBottomDialogFragment.Listener() {
                    @Override
                    public void onViewClicked(int position, View view) {
                        switch (view.getId()) {
                            case R.id.deleteBtn:
                                doDelete(position);
                                break;
                        }
                    }
                })
                .build();

        cancelPublishDialog = new AlertDialog.Builder(UploadFilePage.this)
                .setTitle(R.string.delete)
                .setMessage(R.string.are_you_sure_you_cancel_the_release_of_the_video)
                .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            TransactionManager.callInTransaction(DBHelper.getInstance().getConnectionSource(), new Callable<Object>() {
                                @Override
                                public Object call() throws Exception {
                                    List<UploadFile> uploadFileList = uploadFileAdapter.getData();
                                    for (UploadFile uploadFile : uploadFileList) {
                                        UpdateBuilder<UploadFile, Integer> updateBuilder = uploadFileDao.updateBuilder();
                                        updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
                                        updateBuilder.where().eq(UploadFile.ID_FIELD, uploadFile.getId());
                                        updateBuilder.update();
                                    }
                                    switch (parentType) {
                                        case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC: {
                                            UpdateBuilder<SpecialTopicDBModel, String> updateBuilder = SpecialTopicDBModelDao.getDao().updateBuilder();
                                            updateBuilder.updateColumnValue(SpecialTopicDBModel.CONTROL_OPTION, PublishUtil.CONTROL_OPTION_DELETE);
                                            updateBuilder.where().eq(SpecialTopicDBModel.ID, parentId);
                                            updateBuilder.update();
                                            break;
                                        }

                                        case PublishUtil.PARENT_TYPE_INFO: {
                                            UpdateBuilder<InfoModel, String> updateBuilder = InfoModelDao.getDao().updateBuilder();
                                            updateBuilder.updateColumnValue(InfoModel.CONTROL_OPTION, PublishUtil.CONTROL_OPTION_DELETE);
                                            updateBuilder.where().eq(InfoModel.ID, parentId);
                                            updateBuilder.update();
                                            break;
                                        }
                                        case PublishUtil.PARENT_TYPE_LEARN_VIDEO: {
                                            UpdateBuilder<LearnVideoModel, String> updateBuilder = LearnVideoModelDao.getDao().updateBuilder();
                                            updateBuilder.updateColumnValue(LearnVideoModel.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
                                            updateBuilder.where().eq(LearnVideoModel.ID_FIELD, parentId);
                                            updateBuilder.update();
                                            break;
                                        }
                                        case PublishUtil.PARENT_TYPE_NORMAL_VIDEO: {
                                            UpdateBuilder<NormalVideoModel, String> updateBuilder = NormalVideoModelDao.getDao().updateBuilder();
                                            updateBuilder.updateColumnValue(NormalVideoModel.CONTROL_OPTION, PublishUtil.CONTROL_OPTION_DELETE);
                                            updateBuilder.where().eq(NormalVideoModel.ID, parentId);
                                            updateBuilder.update();
                                            break;
                                        }
                                        case PublishUtil.PARENT_TYPE_PHOTO: {
                                            UpdateBuilder<PhotoModel, String> updateBuilder = PhotoModelDao.getDao().updateBuilder();
                                            updateBuilder.updateColumnValue(PhotoModel.CONTROL_OPTION, PublishUtil.CONTROL_OPTION_DELETE);
                                            updateBuilder.where().eq(PhotoModel.ID, parentId);
                                            updateBuilder.update();
                                            break;
                                        }
                                    }
                                    return null;
                                }
                            });
                        } catch (SQLException e) {
                            Timber.e("onClick:" + Log.getStackTraceString(e));
                        }
                        PublishUtil.sendPublishProgressStatusUpdateBroadcast();
                        finish();
                    }
                })
                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create();
        uploadFileAdapter = new UploadFileAdapter();
        recyclerView.setItemAnimator(null);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        uploadFileAdapter.bindToRecyclerView(recyclerView);
        uploadFileAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                UploadFile uploadFile = uploadFileAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.statusMask: {
                        try {

                            switch (uploadFile.getControlOption()) {
                                case PublishUtil.CONTROL_OPTION_FAILED_PAUSE:
                                case PublishUtil.CONTROL_OPTION_PAUSE:
                                    UploadFileDao.resumeById(uploadFile.getId());
                                    PublishUtil.resumeParent(parentType, parentId);
                                    break;
                                case PublishUtil.CONTROL_OPTION_UPLOAD:
                                    UploadFileDao.pauseById(uploadFile.getId());
                                    break;
                            }
                            uploadFileDao.refresh(uploadFile);
                        } catch (SQLException e) {
                            Timber.e("onItemChildClick:" + Log.getStackTraceString(e));
                        }
                        uploadFileAdapter.setData(position, uploadFile);
                        boolean allPaused = true;
                        for (UploadFile item : uploadFileAdapter.getData()) {
                            if (item.getControlOption() != PublishUtil.CONTROL_OPTION_PAUSE) {
                                allPaused = false;
                                break;
                            }
                        }
                        if (allPaused) {
                            PublishUtil.pauseParent(parentType, parentId);
                            PublishUtil.sendPublishProgressStatusUpdateBroadcast();
                        }
                        break;
                    }
                    case R.id.deleteBtn:
                        deleteDialog.setPosition(position);
                        deleteDialog.show(getSupportFragmentManager(), "");
                        break;
                }
            }
        });
        titleTv.setText(R.string.upload_label);
        titleActionBtn.setText(R.string.delete_all);
        uploadFileDao = UploadFileDao.getDao();
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                try {
                    String action = intent.getAction();
                    switch (action) {
                        case PublishUtil.ACTION_TRANSCODE_UPDATE:
                        case PublishUtil.ACTION_UPLOAD_UPDATE:
                            query();
                            break;
                        case PublishUtil.ACTION_PUBLISH_PROGRESS_STATUS_UPDATE:
                            switch (parentType) {
                                case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC: {
                                    SpecialTopicDBModel specialTopicDBModel = SpecialTopicDBModelDao.getDao().queryForId(parentId);
                                    switch (specialTopicDBModel.getProgressStatus()) {
                                        case PublishUtil.PROGRESS_STATUS_IN_REVIEW:
                                        case PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                                            finish();
                                            break;
                                    }

                                    break;
                                }
                                case PublishUtil.PARENT_TYPE_INFO: {
                                    InfoModel infoModel = InfoModelDao.getDao().queryForId(parentId);
                                    switch (infoModel.getProgressStatus()) {
                                        case PublishUtil.PROGRESS_STATUS_IN_REVIEW:
                                        case PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                                            finish();
                                            break;
                                    }

                                    break;
                                }
                                case PublishUtil.PARENT_TYPE_NORMAL_VIDEO: {
                                    NormalVideoModel normalVideoModel = NormalVideoModelDao.getDao().queryForId(parentId);
                                    switch (normalVideoModel.getProgressStatus()) {
                                        case PublishUtil.PROGRESS_STATUS_IN_REVIEW:
                                        case PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                                            finish();
                                            break;
                                    }
                                    break;
                                }

                                case PublishUtil.PARENT_TYPE_LEARN_VIDEO: {
                                    LearnVideoModel learnVideoModel = LearnVideoModelDao.getDao().queryForId(parentId);
                                    switch (learnVideoModel.getProgressStatus()) {
                                        case PublishUtil.PROGRESS_STATUS_IN_REVIEW:
                                        case PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                                            finish();
                                            break;
                                    }
                                    break;
                                }

                                case PublishUtil.PARENT_TYPE_PHOTO: {
                                    PhotoModel photoModel = PhotoModelDao.getDao().queryForId(parentId);
                                    switch (photoModel.getProgressStatus()) {
                                        case PublishUtil.PROGRESS_STATUS_IN_REVIEW:
                                        case PublishUtil.PROGRESS_STATUS_NOT_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_PUBLISHED:
                                        case PublishUtil.PROGRESS_STATUS_SERVER_TRANSCODING:
                                            finish();
                                            break;
                                    }
                                    break;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                } catch (Exception e) {

                }
            }
        };
        query();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PublishUtil.ACTION_TRANSCODE_UPDATE);
        intentFilter.addAction(PublishUtil.ACTION_UPLOAD_UPDATE);
        intentFilter.addAction(PublishUtil.ACTION_PUBLISH_PROGRESS_STATUS_UPDATE);
        registerReceiver(broadcastReceiver, intentFilter);
        PublishUtil.startPublish();
    }

    @Override
    protected void onDestroy() {
        try {
            unregisterReceiver(broadcastReceiver);
        } catch (Exception e) {

        }
        super.onDestroy();
    }

    public void query() {
        if (querying) return;
        querying = true;
        List<UploadFile> uploadFileList = new LinkedList<>();
        try {
            switch (parentType) {
                case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC:
                case PublishUtil.PARENT_TYPE_INFO:
                    uploadFileList.addAll(uploadFileDao.queryBuilder().where()
                            .eq(UploadFile.USER_ID_FIELD, AccountUtil.getCurrentUser().getId())
                            .and()
                            .eq(UploadFile.PARENT_ID_FIELD, parentId)
                            .and()
                            .eq(UploadFile.PARENT_TYPE_FIELD, parentType)
                            .and()
                            .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE)
                            .and()
                            .ne(UploadFile.PROGRESS_STATUS_FIELD, PublishUtil.PROGRESS_STATUS_PRE_UPLOAD_FINISH)
                            .query());
                    break;
                case PublishUtil.PARENT_TYPE_LEARN_VIDEO:
                case PublishUtil.PARENT_TYPE_NORMAL_VIDEO:
                    uploadFileList.addAll(uploadFileDao.queryBuilder().where()
                            .eq(UploadFile.USER_ID_FIELD, AccountUtil.getCurrentUser().getId())
                            .and()
                            .eq(UploadFile.FILE_TYPE_FIELD, PublishUtil.UPLOAD_FILE_TYPE_VIDEO)
                            .and()
                            .eq(UploadFile.PARENT_ID_FIELD, parentId)
                            .and()
                            .eq(UploadFile.PARENT_TYPE_FIELD, parentType)
                            .and()
                            .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE)
                            .and()
                            .ne(UploadFile.PROGRESS_STATUS_FIELD, PublishUtil.PROGRESS_STATUS_PRE_UPLOAD_FINISH)
                            .query());
                    break;
                case PublishUtil.PARENT_TYPE_PHOTO:
                    uploadFileList.addAll(uploadFileDao.queryBuilder().where()
                            .eq(UploadFile.USER_ID_FIELD, AccountUtil.getCurrentUser().getId())
                            .and()
                            .eq(UploadFile.FILE_TYPE_FIELD, PublishUtil.UPLOAD_FILE_TYPE_PHOTO)
                            .and()
                            .eq(UploadFile.PARENT_ID_FIELD, parentId)
                            .and()
                            .eq(UploadFile.PARENT_TYPE_FIELD, parentType)
                            .and()
                            .ne(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE)
                            .and()
                            .ne(UploadFile.PROGRESS_STATUS_FIELD, PublishUtil.PROGRESS_STATUS_PRE_UPLOAD_FINISH)
                            .query());
                    break;
            }


        } catch (Exception e) {
            Log.e(TAG, "query:" + Log.getStackTraceString(e));
        }
        uploadFileAdapter.setNewData(uploadFileList);
        querying = false;
    }

    @OnClick({R.id.titleBackBtn, R.id.titleActionBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.titleBackBtn:
                finish();
                break;
            case R.id.titleActionBtn:
                cancelPublishDialog.show();
                break;
        }
    }

    public void doDelete(int position) {
        UploadFile uploadFile = uploadFileAdapter.getItem(position);
        int deleteLimit = 1;
        switch (parentType) {
            case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC:
                deleteLimit = 1;
                break;
            case PublishUtil.PARENT_TYPE_INFO:
                deleteLimit = 1;
                break;
            case PublishUtil.PARENT_TYPE_LEARN_VIDEO:
                deleteLimit = 2;
                break;
            case PublishUtil.PARENT_TYPE_NORMAL_VIDEO:
                deleteLimit = 1;
                break;
            case PublishUtil.PARENT_TYPE_PHOTO:
                deleteLimit = 1;
                break;
        }

        if (uploadFileAdapter.getData().size() > deleteLimit) {
            try {
                UpdateBuilder<UploadFile, Integer> updateBuilder = uploadFileDao.updateBuilder();
                updateBuilder.where().eq(UploadFile.ID_FIELD, uploadFile.getId());
                updateBuilder.updateColumnValue(UploadFile.CONTROL_OPTION_FIELD, PublishUtil.CONTROL_OPTION_DELETE);
                updateBuilder.update();
                switch (parentType) {
                    case PublishUtil.PARENT_TYPE_INFO: {
                        InfoModel infoModel = InfoModelDao.getDao().queryForId(parentId);
                        List<ArticleNode> articleNodeList = JSON.parseArray(infoModel.getArticleJson(), ArticleNode.class);
                        Iterator<ArticleNode> articleNodeIterator = articleNodeList.iterator();
                        outerLoop:
                        while (articleNodeIterator.hasNext()) {
                            ArticleNode articleNode = articleNodeIterator.next();
                            switch (uploadFile.getFileType()) {
                                case PublishUtil.UPLOAD_FILE_TYPE_VIDEO:
                                    if (articleNode.getType() == ArticleNode.NODE_TYPE_VIDEO && articleNode.getUploadFileId() == uploadFile.getId()) {
                                        articleNodeIterator.remove();
                                        break outerLoop;
                                    }
                                    break;
                                case PublishUtil.UPLOAD_FILE_TYPE_PHOTO:
                                    if (articleNode.getType() == PublishInfoAdapter.NODE_TYPE_PHOTO && articleNode.getUploadFileId() == uploadFile.getId()) {
                                        articleNodeIterator.remove();
                                        break outerLoop;
                                    }
                                    break;
                            }
                        }
                        String infoNodeJson = JSON.toJSONString(articleNodeList);
                        UpdateBuilder<InfoModel, String> builder = InfoModelDao.getDao().updateBuilder();
                        builder.updateColumnValue(InfoModel.ARTICLE_JSON, infoNodeJson);
                        builder.where().eq(InfoModel.ID, parentId);
                        builder.update();
                        break;
                    }

                    case PublishUtil.PARENT_TYPE_SPECIAL_TOPIC: {
                        SpecialTopicDBModel specialTopicDBModel = SpecialTopicDBModelDao.getDao().queryForId(parentId);
                        List<ArticleNode> articleNodeList = JSON.parseArray(specialTopicDBModel.getArticleJson(), ArticleNode.class);
                        Iterator<ArticleNode> articleNodeIterator = articleNodeList.iterator();
                        outerLoop:
                        while (articleNodeIterator.hasNext()) {
                            ArticleNode articleNode = articleNodeIterator.next();
                            switch (uploadFile.getFileType()) {
                                case PublishUtil.UPLOAD_FILE_TYPE_VIDEO:
                                    if (articleNode.getType() == ArticleNode.NODE_TYPE_VIDEO && articleNode.getUploadFileId() == uploadFile.getId()) {
                                        articleNodeIterator.remove();
                                        break outerLoop;
                                    }
                                    break;
                                case PublishUtil.UPLOAD_FILE_TYPE_PHOTO:
                                    if (articleNode.getType() == PublishInfoAdapter.NODE_TYPE_PHOTO && articleNode.getUploadFileId() == uploadFile.getId()) {
                                        articleNodeIterator.remove();
                                        break outerLoop;
                                    }
                                    break;
                            }
                        }
                        String infoNodeJson = JSON.toJSONString(articleNodeList);
                        UpdateBuilder<SpecialTopicDBModel, String> builder = SpecialTopicDBModelDao.getDao().updateBuilder();
                        builder.updateColumnValue(SpecialTopicDBModel.ARTICLE_JSON, infoNodeJson);
                        builder.where().eq(SpecialTopicDBModel.ID, parentId);
                        builder.update();
                        break;
                    }

                    case PublishUtil.PARENT_TYPE_NORMAL_VIDEO: {
                        NormalVideoModel normalVideoModel = NormalVideoModelDao.getDao().queryForId(parentId);
                        UpdateBuilder<NormalVideoModel, String> builder = NormalVideoModelDao.getDao().updateBuilder();
                        builder.updateColumnValue(NormalVideoModel.VIDEO_NUM, normalVideoModel.getVideoNum() - 1 > 0 ? normalVideoModel.getVideoNum() - 1 : 0);
                        builder.where().eq(NormalVideoModel.ID, parentId);
                        builder.update();
                        break;
                    }
                    case PublishUtil.PARENT_TYPE_LEARN_VIDEO: {
                        LearnVideoModel learnVideoModel = LearnVideoModelDao.getDao().queryForId(parentId);
                        UpdateBuilder<LearnVideoModel, String> builder = LearnVideoModelDao.getDao().updateBuilder();
                        builder.updateColumnValue(LearnVideoModel.VIDEO_NUM, learnVideoModel.getVideoNum() - 1 > 0 ? learnVideoModel.getVideoNum() - 1 : 0);
                        builder.where().eq(LearnVideoModel.ID_FIELD, parentId);
                        builder.update();
                        break;
                    }
                    case PublishUtil.PARENT_TYPE_PHOTO:
                        PhotoModel photoModel = PhotoModelDao.getDao().queryForId(parentId);
                        UpdateBuilder<PhotoModel, String> builder = PhotoModelDao.getDao().updateBuilder();
                        builder.updateColumnValue(PhotoModel.PHOTO_NUM, photoModel.getPhotoNum() - 1 > 0 ? photoModel.getPhotoNum() - 1 : 0);
                        List<PublishPhotoItem> publishPhotoItemList = new LinkedList<>();
                        for (int i = 0; i < uploadFileAdapter.getData().size(); ++i) {
                            if (i == position) continue;
                            publishPhotoItemList.add(PublishPhotoItem.newBuilder()
                                    .photoUrl(uploadFileAdapter.getItem(i).getFileLocalUrl())
                                    .build());
                        }
                        builder.updateColumnValue(PhotoModel.COVER, JSON.toJSONString(publishPhotoItemList));
                        builder.where().eq(PhotoModel.ID, parentId);
                        builder.update();
                        break;
                }
            } catch (SQLException e) {
                Timber.e("onItemChildClick:" + Log.getStackTraceString(e));
            }
            uploadFileAdapter.remove(position);
            PublishUtil.sendPublishProgressStatusUpdateBroadcast();
        } else {
            cancelPublishDialog.show();
        }
    }
}
